package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.CoinType;
import cn.net.shuxing.yifu.common.bean.MemberLevel;
import cn.net.shuxing.yifu.common.bean.User;
import cn.net.shuxing.yifu.common.bean.UserCapital;
import cn.net.shuxing.yifu.common.constant.UserType;
import cn.net.shuxing.yifu.common.mapper.MemberLevelMapper;
import cn.net.shuxing.yifu.common.util.CommonExample;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.List;

@Service
public class MemberLevelService extends BaseService<MemberLevel, MemberLevelMapper> {

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserCapitalService userCapitalService;

    @Autowired
    private CoinTypeService coinTypeService;

    public int insert(int coinTypeId, int level, BigDecimal low, BigDecimal high, String desc) {
        MemberLevel memberLevel = new MemberLevel();
        memberLevel.setLevel(level);
        memberLevel.setCoinTypeId(coinTypeId);
        memberLevel.setLow(low);
        memberLevel.setHigh(high);
        memberLevel.setDescription(desc);
        return insert(memberLevel);
    }

    @Override
    public int insert(MemberLevel bean) {
        Integer level = bean.getLevel();
        if (level <= 1) {
            bean.setLevel(1);
            bean.setLow(new BigDecimal(0));
        } else {
            MemberLevel memberLevel = getByCoinTypeAndLevel(bean.getCoinTypeId(), bean.getLevel() - 1);
            if (memberLevel == null)
                return -1;
            bean.setLow(memberLevel.getHigh());
        }
        return super.insert(bean);
    }

    public void addLevel(int level, int coinTypeId, BigDecimal low, String desc) {
        if (level <= 1)
            insert(coinTypeId, 1, low, new BigDecimal(-1), desc);
        else {
            MemberLevel memberLevel = getByCoinTypeAndLevel(coinTypeId, level - 1);
            if (memberLevel.getHigh().compareTo(new BigDecimal(-1)) != 0)
                return;
            else {
                memberLevel.setHigh(low);
                update(memberLevel);
                insert(coinTypeId, level, low, new BigDecimal(-1), desc);
            }
        }
    }

    public MemberLevel getByCoinTypeAndLevel(Integer coinTypeId, Integer level) {
        MemberLevel memberLevel = new MemberLevel();
        memberLevel.clearCreateTimeAndUpdateTime();
        memberLevel.setLevel(level);
        memberLevel.setCoinTypeId(coinTypeId);
        return get(memberLevel);
    }

    public int calcaulateMemberLevel(Integer userId) {
        User user = userService.getByPrimaryKeyWithoutComplete(userId);
        if (user == null)
            return 0;
        boolean flag = userService.hasRole(userId, UserType.MEMBER);
        if (!flag)
            return 0;
        List<UserCapital> userCapital = userCapitalService.getByUserIdAndUserTypeWithoutComplete(userId, UserType.MEMBER);
        BigDecimal totalDealCount = new BigDecimal(0);

        if (userCapital == null || userCapital.isEmpty()) {
            return 0;
        } else {
            for (UserCapital capital : userCapital) {
                totalDealCount = totalDealCount.add(capital.getTotalDealCount());
            }
        }

        Example example = new Example(MemberLevel.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLessThan("low", totalDealCount);
        criteria.andGreaterThanOrEqualTo("high", totalDealCount);
        MemberLevel memberLevel = getByExample(example);
        if (memberLevel == null)
            return 0;
        return memberLevel.getLevel();
    }

//    public Integer calcaulateMemberLevel(Integer userId)
//    {
//        Integer min = 100;
//        List<CoinType> all = coinTypeService.getAllWithoutComplete();
//        for (CoinType coinType : all) {
//            Integer coinTypeId = coinType.getId();
//            int level = calcaulateMemberLevel(userId, coinTypeId);
//            if (level != -1)
//                min = (min>level)?level:min;
//        }
//        if (min == 100)
//            min = null;
//        return min;
//    }

    public void calculateMemberLevelCount() {
        List<Integer> integers = userRoleService.getsByUserRole(UserType.MEMBER);
        List<MemberLevel> all = getAll();
        for (MemberLevel memberLevel : all) {
            Integer level = memberLevel.getLevel();
            Integer newCount = 0;
            if (integers != null && !integers.isEmpty()) {
                for (Integer integer : integers) {
                    int i = calcaulateMemberLevel(integer);
                    if (level == i) {
                        newCount += 1;
                    }
                }
            }
            memberLevel.setCount(newCount);
            update(memberLevel);
        }
    }
}
